package com.example.hotel.web;

import com.example.hotel.common.JsonResult;
import com.example.hotel.dao.ResidentDao;
import com.example.hotel.entity.Resident;
import com.example.hotel.service.ResidentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;
import java.util.List;


    @Api(tags = "住户信息管理")
    @RestController
    @CrossOrigin
    @RequestMapping("resident")
    public class ResidentController {

        @Autowired
        ResidentService service ;

        @ApiOperation(value = "获取所有住户信息")
        @GetMapping("findAll")
        public JsonResult<List<Resident>> findAll()
        {
            List<Resident> list = service.findAll() ;
            return JsonResult.ok(list) ;
        }

        @ApiOperation(value = "添加住户")
        @GetMapping("add")
        public JsonResult insert(Resident resident)
        {
            try {
                int r = service.add(resident);
                if(r > 0){
                    return JsonResult.ok() ;
                }else{
                    return JsonResult.fail(5001,"住户信息添加失败") ;
                }

            }catch (Exception e){
                e.printStackTrace();
                return JsonResult.fail() ;
            }

        }


        @ApiOperation(value = "修改住户")
        @GetMapping("update")
        public JsonResult update(int customId ,String state)
        {
            try {
                int r = service.update(customId, state);
                if(r > 0){
                    return JsonResult.ok() ;
                }else{
                    return JsonResult.fail(5001,"住户信息修改失败") ;
                }

            }catch (Exception e){
                e.printStackTrace();
                return JsonResult.fail() ;
            }

        }
        @ApiOperation(value = "删除住户")
        @DeleteMapping ("delete")
        public JsonResult delete( int customId)
        {
            try {
                int r = service.delete(customId);
                if(r > 0){
                    return JsonResult.ok() ;
                }else{
                    return JsonResult.fail(5001,"删除住户信息失败") ;
                }

            }catch (Exception e){
                e.printStackTrace();
                return JsonResult.fail() ;
            }

        }

}
